package com.xn.app.muying.adpter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.activity.BabyStudyListActivity;
import com.xn.app.muying.bean.BabyStudyListBean;
import com.xn.app.muying.util.HttpUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/10.
 */
public class BabyStudyListAdpter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<BabyStudyListBean> data = new ArrayList();
    private Handler handler = new Handler();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public BabyStudyListAdpter(Context context, ImageLoader imageLoader, DisplayImageOptions options) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.imageLoader = imageLoader;
        this.options = options;
    }


    public void getData(ArrayList<BabyStudyListBean> data) {
        this.data=data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        if (view == null) {
            view = inflater.inflate(R.layout.activity_babystudylist_item, null);
            holder.iv_picture = (ImageView) view.findViewById(R.id.activity_babystudylist_item_picture);
            holder.tv_title = (TextView) view.findViewById(R.id.activity_babystudylist_item_title);
            holder.tv_content = (TextView) view.findViewById(R.id.activity_babystudylist_item_content);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
            holder.tv_title.setText(data.get(position).getTitle());
            holder.tv_content.setText(data.get(position).getShortDescription());
            holder.iv_picture.setTag(data.get(position).getPictureUrl());
        //设置图片，如果内存中有，优先从内存中读取
        holder.iv_picture.setImageResource(0);
        if (data.get(position).getPictureUrl() != null && !data.get(position).getPictureUrl().equals("null")) {
            imageLoader.displayImage(data.get(position).getPictureUrl(), holder.iv_picture, options);
        }
        return view;
    }

    class ViewHolder {
        ImageView iv_picture;
        TextView tv_title, tv_content;
    }
}
